﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FineAdmin.Common;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.Web.Controllers;

namespace FineAdmin.Web.Areas.Shop.Controllers
{
    public class CategorySetController  : BaseController
    {
        // GET: Shop/CategorySet
        public ICategoryService CategoryService { get; set; }
        public override ActionResult Index(int? id)
         {
            
            base.Index(id);
            return View();
        }

        [HttpGet]
        public JsonResult List()
        {
            var list = CategoryService.GetCategoryList();
            var result = new { code = 0, count = list.Count(), data = list };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetCategoryTreeSelect()
        {
            var result = CategoryService.GetCategoryTreeSelect();
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(CategoryModel model)
        {
            model.CreateTime = DateTime.Now;
            model.CreateUserId = Operator.UserId;
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = CategoryService.Insert(model) ? SuccessTip("添加成功") : ErrorTip("添加失败");
            return Json(result);
        }

        public ActionResult Edit(int id)
        {
            
            var model = CategoryService.GetById(id);
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(CategoryModel model)
        {
            if (!model.Pid.HasValue) {
                model.Pid = 0;
            }
            if (!model.ShowPhone.HasValue) {
                model.ShowPhone = false;
            }
            if (!model.ShowWeb.HasValue)
            {
                model.ShowWeb = false;
            }
            model.UpdateTime = DateTime.Now;
            model.UpdateUserId = Operator.UserId;
            var result = CategoryService.UpdateById(model) ? SuccessTip("修改成功") : ErrorTip("修改失败");
            return Json(result);
        }

        [HttpGet]
        public JsonResult Delete(int id)
        {
            var result = CategoryService.DeleteById(id) ? SuccessTip("删除成功") : ErrorTip("删除失败");
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [HttpGet]
        public JsonResult CreateLink()
        {
            var result = CategoryService.CreateLink()>0 ? SuccessTip("成功") : ErrorTip("失败");
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}